﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Shapes;
using System.Windows.Media;

namespace VanGuideWP7.Controls
{
    public partial class RatingsControl : UserControl
    {
        public static SolidColorBrush LIT_COLOR = CombineAlphaAndColorInSolidColorBrush( 1.0, "#FFFF00");
        public static SolidColorBrush UNLIT_COLOR = CombineAlphaAndColorInSolidColorBrush( 1.0,"#C0C0C0");

        public delegate void RatingSelectedEventHandler(object sender, int rating);

        public event RatingSelectedEventHandler RatingSelected;

        public RatingsControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// The <see cref="Rating" /> dependency property's name.
        /// </summary>
        public const string RatingPropertyName = "Rating";

        /// <summary>
        /// Gets or sets the value of the <see cref="Rating" />
        /// property. This is a dependency property.
        /// </summary>
        public int Rating
        {
            get
            {
                return (int)GetValue(RatingProperty);
            }
            set
            {
                SetValue(RatingProperty, value);
                
            }
        }

        /// <summary>
        /// Identifies the <see cref="Rating" /> dependency property.
        /// </summary>
        public static readonly DependencyProperty RatingProperty = DependencyProperty.Register(
            RatingPropertyName,
            typeof(int),
            typeof(RatingsControl),
            new PropertyMetadata(new PropertyChangedCallback(RatingChangedCallback)));
       
        private static void RatingChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RatingsControl r = d as RatingsControl;
            r.updateUi((int)e.NewValue);
 
        }

        private void updateUi(int rating)
        {
            if (rating >= 1) FirstStar.Fill = LIT_COLOR;
            if (rating >= 2) SecondStar.Fill = LIT_COLOR;
            if (rating >= 3) ThirdStar.Fill = LIT_COLOR;
            if (rating >= 4) FourthStar.Fill = LIT_COLOR;
            if (rating >= 5) FifthStar.Fill = LIT_COLOR;
        }
        //TODO: Should probably move this to it's own class
        #region protected static SolidColorBrush CombineAlphaAndColorInSolidColorBrush(double opacity, string color)
		/// <summary>
		/// adds the alpha (opacity) value to the front of the color string
		/// </summary>
		/// <param name="opacity"></param>
		/// <param name="color"></param>
		/// <returns></returns>
		protected static SolidColorBrush CombineAlphaAndColorInSolidColorBrush(double opacity, string color)
		{
			SolidColorBrush theAnswer = new SolidColorBrush();

			// //////////////////////////////////////////////////////
			// deal with opacity

			if (opacity > 1.0)
				opacity = 1.0;

			if (opacity < 0.0)
				opacity = 0.0;

			// get the hex value of the alpha chanel (opacity):
			byte a = (byte)(Convert.ToInt32(255 * opacity));

			// --cmt:5afff4b1-daca-4daf-ab56-64864c80235e--
			// deal with the color

			try
			{
				byte r = (byte)(Convert.ToUInt32(color.Substring(1, 2), 16));
				byte g = (byte)(Convert.ToUInt32(color.Substring(3, 2), 16));
				byte b = (byte)(Convert.ToUInt32(color.Substring(5, 2), 16));

				theAnswer.Color = Color.FromArgb(a, r, g, b);				
			}
			catch
			{
				// pick a fugly color, but don't cause the system to barf.
				theAnswer.Color = Color.FromArgb(255, 255, 0, 0);
			}



			return theAnswer;
		}
		#endregion

        private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Canvas star = sender as Canvas;

            int r = GetRatingByCanvas( star );

            if (RatingSelected != null)
                RatingSelected(this, r);

        }

        private int GetRatingByCanvas(Canvas star)
        {
            switch (star.Name)
            {
                case "cFirstStar":
                    return 1;
                case "cSecondStar":
                    return 2;
                case "cThirdStar":
                    return 3;
                case "cFourthStar":
                    return 4;
                case "cFifthStar":
                    return 5;
            }

            return 0;
        }
    }
}
